<template>
  <el-card style="max-width: 99%">
    <template #header>
      <div class="card-header">
        <span>船票管理</span>
        <el-button type="primary" style="height: 36px;float: right" @click="openTc(1)">添加船票</el-button>
      </div>
    </template>
    <el-row style="margin-bottom: 20px">
      <el-col :span="9" style="display:flex;">
        <el-input v-model="origin" placeholder="出发地" class="i1" />
        <span style="color:#E4E7ED">——</span>
        <el-input v-model="destination" placeholder="目的地" class="i2" />
      </el-col>
      <el-col :span="10">
        <el-button type="primary" @click="cx()">搜索</el-button>
        <el-button @click="cz()">重置</el-button>
      </el-col>
    </el-row>
    <el-table :data="list" border style="margin-bottom: 15px">
      <el-table-column prop="originName" label="出发地" width="180" />
      <el-table-column prop="destinationName" label="目的地" width="180" />
      <el-table-column prop="boatId" label="轮船编号" width="180" />
      <el-table-column prop="number" label="余票" />
      <el-table-column prop="price" label="价格" />
      <el-table-column label="操作" >
        <template #default="scope">
          <el-button :icon="Edit" circle @click="openTc(2,scope.row.id)"/>
<!--          <el-button :icon="SemiSelect" circle  @click="open(scope.row.id)"/>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 0 auto;width: 300px">
      <el-pagination @current-change="handleCurrentChange"
          size="small" background layout="prev, pager, next" :total=count :page-size=pageSize class="mt-4"/>
    </div>
  </el-card>
  <el-dialog v-model="dialogFormVisible" :before-close="qx" :title="this.type==1?'添加船票':'编辑'" width="500">
    <el-form :model="boatForm" style="margin-right: 60px;margin-left: 60px">
      <el-form-item label="价格" prop="price">
        <el-input v-model="boatForm.price" autocomplete="off" />
      </el-form-item>
      <el-form-item label="出发地" prop="beginning">
        <el-select v-model="boatForm.beginning" placeholder="--请选择--">
          <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-select v-model="boatForm.destination" placeholder="--请选择--">
          <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="轮船编号" prop="boatId">
        <el-input v-model="boatForm.boatId" autocomplete="off" />
      </el-form-item>
      <el-form-item label="余票" prop="number">
        <el-input v-model="boatForm.number" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="qx()">取消</el-button>
        <el-button type="primary" @click="qd()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {selectAllBoat, selectProvince} from "@/api/vue3.js"
import {deleteBoat} from "@/api/vue3.js"
import {addBoat} from "@/api/vue3.js"
import {updateBoat} from "@/api/vue3.js"
import {selectByIdBoat} from "@/api/vue3.js"
import {ElMessage, ElMessageBox} from "element-plus";
import {Edit, SemiSelect} from "@element-plus/icons-vue";
export default {
  name: 'Ship',
  computed: {
    SemiSelect() {
      return SemiSelect
    },
    Edit() {
      return Edit
    }
  },
  data(){
    return {
      dialogFormVisible: false,//对话框显示
      type:0,//对话框类型
      origin:'',//出发地
      destination:'',//目的地
      pageNum:1,//当前页码
      pageSize:4,//每页显示条数
      count:0,//总条数
      list:[],//飞机列表
      boatForm:{},
      areaList:[],//地域列表
      cid:'',//地域id
    }
  },
  mounted() {
    this.selList()
    this.dq()
  },
  methods: {
    dq(){
      selectProvince(this.cid).then(res=>{
        console.log(res)
        this.areaList=res.data.data
      })
    },
    cx() {//查询
      this.origin
      this.destination
      this.selList()
    },
    cz() {//重置
      this.origin = ''
      this.destination = ''
      this.selList()
    },
    selList() {
      selectAllBoat(this.origin, this.destination, this.pageNum, this.pageSize).then(res => {
        this.list = res.data.data.list
        this.count = res.data.data.count
      })
    },
    //打开弹窗
    openTc(type, id) {
      this.type = type
      this.dialogFormVisible = true
      if (this.type == 2) {
        selectByIdBoat(id).then(res => {
          this.boatForm = res.data.data.data
        })
      }
    },
    handleCurrentChange(val) {//当前页码改变的事件
      this.pageNum = val;
      this.selList()
    },
    open(id) {
      ElMessageBox.confirm(
          '是否删除该条信息',
          '消息提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      )
          .then(() => {
            deleteBoat(id).then(res=>{
              if (res.data.data.count){
                ElMessage({
                  type: 'success',
                  message: '删除成功',
                })
                this.selList()
              }else {
                ElMessage({
                  type: 'success',
                  message: '删除失败',
                })
              }
            })
          })
          .catch(() => {
            ElMessage({
              type: 'info',
              message: '删除已取消',
            })
          })
    },
    qd(){//确定
      if (this.type==1){
        addBoat(this.boatForm).then(res=>{
          if (res.data.data.count==200){
            ElMessage({
              type: 'success',
              message: '添加成功',
            })
            this.boatForm={}
            this.dialogFormVisible= false
            this.selList()
          }else {
            this.dialogFormVisible=false
            ElMessage({
              type: 'success',
              message: '添加失败',
            })
          }
        })
      }
      if (this.type==2) {
        updateBoat(this.boatForm).then(res => {
          console.log(res)
          if (res.data.data.count == 200) {
            ElMessage({
              type: 'success',
              message: '修改成功',
            })
            this.dialogFormVisible = false
            this.selList()
          } else {
            this.dialogFormVisible = false
            ElMessage({
              type: 'success',
              message: '修改失败',
            })
          }
        })
      }
    },
    qx(){//取消
      this.boatForm={}
      this.dialogFormVisible= false
    }
  }
}
</script>

<style scoped>
.el-card{
  width: 100%;
  height: 600px;
  background-color: white;
  margin-top: 20px;
  border-radius: 30px;
}
.el-table{
  margin: 0 auto;
  width: 96%;
}
.i1{
  margin-left: 25px;
  margin-right: 10px;
}
.i2{
  margin-left: 10px;
  margin-right: 20px;
}
</style>